package client

import (
	"gitee.com/Gogo-gitee/registry"
	"log"
)

//DiscoveryNode 监听zookeeper的变动，并且自动更新注册表
func DiscoveryNode(host, parent string, exit chan struct{}, registryTable *RegistryTable) {

	zooKeeperRegistry := registry.ZooKeeperRegistry{
		Host: []string{host},
	}
	discoveryNodes, errors := zooKeeperRegistry.Discovery(parent)
	go func() {
		for {
			select {
			case err := <-errors:
				log.Fatalf("chanNodes occur error, info: %s", err.Error())
			case changeNodes := <-discoveryNodes:

				registryTable.RenewTable(changeNodes)
				log.Printf("node list changed, new list: %v", changeNodes)
			case <-exit:
				return
			}

		}
	}()

}
